/**
 * https://leetcode.cn/problems/sum-root-to-leaf-numbers/
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumNumbers(TreeNode root) {
        return dfs(root,  0);
    } 
    public int dfs(TreeNode root, int preSum) {
        preSum = (preSum * 10) + root.val;
        if(root.left == null && root.right == null) return preSum;

        int left = 0;
        int right = 0;
        if(root.left != null)
        left = dfs(root.left, preSum);
        if(root.right != null)
        right = dfs(root.right, preSum);

        return left + right;
    }
}